package project.main;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

import project.crawler.parser.QuestionParser;
import project.dbaccesslayer.MySQLAccessLayer;
import project.dbaccesslayer.Table_Questions_To_Ignore;
import project.dbaccesslayer.Table_SO_Answers;
import project.dbaccesslayer.Table_SO_Questions;
import project.dbaccesslayer.Table_SO_Questions_Raw_html;
import project.model.adt.Question;

public class ParseQuestions {

	public static void main(String[] args) throws SQLException {
		if (args.length != 1) {
			System.out.println("Usage: ParseQuestions <limit>");
			return;
		}
		int limit = Integer.parseInt(args[0]);

		Connection conn = MySQLAccessLayer.openConnectionToMySql();

		while (limit > 0) {
			Map<Long, String> map = Table_SO_Questions_Raw_html
					.getQuestionsToParse(conn);
			System.out.println("Got " + map.size() + " questions to parse.");

			if (map.isEmpty()) {
				break;
			}

			for (long questionID : map.keySet()) {
				System.out.println("Parsing question " + questionID);
				Question q = null;
				try {
					q = QuestionParser.parseQuestion(map.get(questionID), questionID);
				} catch (Exception ex) {
					System.out.println(ex);
					ex.printStackTrace();
					Table_Questions_To_Ignore.saveQuestionId(conn, questionID,
							ex.toString());
					continue;
				}
				Table_SO_Questions.saveQuestion(conn, q);
				Table_SO_Answers.saveAnswers(conn, q.answers, q.id);
			}

			limit -= map.size();
		}

		System.out.println("Done.");

		conn.close();
	}

}
